package common.kit;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.jfinal.ext.kit.DateKit;

public class DateKits extends DateKit{
	public static final String MINITE = "yyyy-MM-dd HH:mm";
	
	public static String getDays(Date date) {
		Long times = System.currentTimeMillis() - date.getTime() ;
		return getDurationOfDay(times);
	}
	
	public static String toStr(Date date, String pattern) {
		if (null == date) {
			return "" ;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}
	
	public static String toStr(Date date) {
		return toStr(date, DateKit.datePattern);
	}
	
	public static String getDurationOfDay(Long duration) {
		if (null == duration || duration == 0) {
			return "" ;
		}
		int ss = 1000;
		int mi = ss * 60;
		int hh = mi * 60;
		int dd = hh * 24;
			 
		long day = duration / dd;
		long hour = (duration - day * dd) / hh;
		long minute = (duration - day * dd - hour * hh) / mi;
		long second = (duration - day * dd - hour * hh - minute * mi) / ss;
		
		if (day > 0) {
			return day + "天前" ;
		}
		if (hour > 0) {
			return hour + "小时前" ;
		}
		if (minute > 0) {
			return minute + "分钟前" ;
		}
		return second + "秒前" ;
	}
	
	public static String getDuration(Long duration) {
		if (null == duration || duration == 0) {
			return "" ;
		}
		StringBuilder strBuilder = new StringBuilder();
		int ss = 1000;
		int mi = ss * 60;
		int hh = mi * 60;
		int dd = hh * 24;
			 
		long day = duration / dd;
		long hour = (duration - day * dd) / hh;
		long minute = (duration - day * dd - hour * hh) / mi;
		long second = (duration - day * dd - hour * hh - minute * mi) / ss;
		
		strBuilder.append(doubleByZero(day,"天"))
		.append(doubleByZero(hour,"小时"))
		.append(doubleByZero(minute,"分钟"))
		.append(doubleByZero(second,"秒"));
		return strBuilder.toString() ;
	}
	
	public static String doubleByZero(Long number , String time) {
		if (number == 0) {
			return "" ;
		}
		return number + time ;
	}
	
	public static String getPrevPeriodStr() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -3);
		int month = calendar.get(Calendar.MONTH) ; //从0开始
		int period = (int) Math.floor(month/3)  ;
		String[] arr = {"第一季度","第二季度","第三季度","第四季度"};
		return DateKits.toStr(calendar.getTime() , "yyyy年 ") + arr[period] ;
	}
	
	public static String getWeekName() {
		return getWeekName(new Date());
	}
	
	public static String getWeekName(Date date) {
		Calendar calendar = Calendar.getInstance();  
		calendar.setFirstDayOfWeek(Calendar.MONDAY);  
		calendar.setTime(date);
		int week = calendar.get(Calendar.WEEK_OF_YEAR);
		return DateKit.toStr(date , "YYYY年") + "第" + week + "周" ;
	}
	
	public static Date getPrevPeriodStart() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -3);
		int month = calendar.get(Calendar.MONTH) ; //从0开始
		int period = (int) Math.floor(month/3)  ;
		String[] arr = {"01-01 00:00:00","04-01 00:00:00","07-01 00:00:00","10-01 00:00:00"};
		return DateKit.toDate(DateKits.toStr(calendar.getTime() , "yyyy-") + arr[period]) ;
	}
	
	public static Date getPrevPeriodEnd() {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH, -3);
		int month = calendar.get(Calendar.MONTH) ; //从0开始
		int period = (int) Math.floor(month/3)  ;
		String[] arr = {"03-31 23:59:59","06-30 23:59:59","09-30 23:59:59","12-31 23:59:59"};
		return DateKit.toDate(DateKits.toStr(calendar.getTime() , "yyyy-") + arr[period]) ;
	}
	
	public static String getStartTime(String startTime) {
		return startTime + " 00:00:00" ;
	}
	
	public static String getEndTime(String endTime) {
		return endTime + " 23:59:59" ;
	}
	
	public static String parseStartTime(Date startTime) {
		return DateKits.getStartTime( DateKits.toStr(startTime));
	}
	
	public static String parseEndTime(Date endTime) {
		return DateKits.getEndTime( DateKits.toStr(endTime) );
	}
	
	public static int getMixDays(Date startDate , Date endDate) {
		return (int) ((endDate.getTime() - startDate.getTime()) / (1000*3600*24));
	}
	
	public static Date getDateByDay(Date date , int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.DATE, day);
		return calendar.getTime() ;
	}
	
	public static int getDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	
	public static int getLastDayOfMonth(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	public static Date addMonth(Date date , int months) {
		Calendar calendar = Calendar.getInstance() ;
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, months);
		return calendar.getTime();
	}
	
	public static Date addMonth( int months) {
		return DateKits.addMonth(new Date() , months);
	}
	
	public static Date addYear(Date date , int year) {
		Calendar calendar = Calendar.getInstance() ;
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		return calendar.getTime();
	}
	
	public static Date addYear( int year) {
		return DateKits.addYear(new Date() , year);
	}
	
	public static Date addDay(Date date , int days) {
		Calendar calendar = Calendar.getInstance() ;
		calendar.setTime(date);
		calendar.add(Calendar.DATE, days);
		return calendar.getTime();
	}
	
	public static Date addDay( int days) {
		return DateKits.addDay(new Date() , days);
	}
	
	public static Date addMin(Date date , int min) {
		Calendar calendar = Calendar.getInstance() ;
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, min);
		return calendar.getTime();
	}
	
	public static Date addMin( int min) {
		return DateKits.addMin(new Date() , min);
	}
	
	public static Date addHour(Date date , int hour) {
		Calendar calendar = Calendar.getInstance() ;
		calendar.setTime(date);
		calendar.add(Calendar.HOUR, hour);
		return calendar.getTime();
	}
	
	public static Date addHour( int hour) {
		return DateKits.addHour(new Date() , hour);
	}
	
	/**
	 * 比较两个日期是否相等
	 * @format yyyy-MM-dd
	 */
	public static boolean isSameDate(Date date , Date secDate) {
		if (null == date || null == secDate) {
			return false ;
		}
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return fmt.format(date).equals(fmt.format(secDate));
		} catch (Exception e) {
			return false ;
		}
	}
	
	public static boolean isUsableWeek(Date date , String weeks) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int weekday = calendar.get(Calendar.DAY_OF_WEEK);
		String weekName = "" ;
		switch (weekday) {
		case 1:
			weekName = "周日" ;
			break ;
		case 2:
			weekName = "周一" ;
			break ;
		case 3:
			weekName = "周二" ;
			break ;
		case 4:
			weekName = "周三" ;
			break ;
		case 5:
			weekName = "周四" ;
			break ;
		case 6:
			weekName = "周五" ;
			break ;
		case 7:
			weekName = "周六" ;
			break ;
		}
		return weeks.contains(weekName) ;
	}
	
	public static boolean notLessThanStartDate(Date date , Date startDate) {
		if (DateKits.isSameDate(date, startDate)) {
			return true ;
		}
		return date.after(startDate);
	}
	
	public static boolean notMoreThanEndDate(Date date , Date endDate) {
		if (DateKits.isSameDate(date, endDate)) {
			return true ;
		}
		return date.before(endDate);
	}
	
	public static Long getTheDateTime( Date date , int hour , int min) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MINUTE, min);
		calendar.set(Calendar.SECOND , 59 );
		return calendar.getTimeInMillis();
	}
	
	public static boolean notLessThanToday(Date date , int aheadDays, int aheadHour , int aheadMin) {
		Date today = DateKits.addDay(aheadDays);
		if (DateKits.isSameDate(date, today)) {
			return DateKits.getTheDateTime( today , aheadHour, aheadMin) >= date.getTime() ;
		}
		return date.after(today);
	}
	
}
